<template>
  <div style="margin-left: 15px">
    <span>{{ service.name }}</span>
    <p></p>
    <div>
      <a-radio-group v-model="service.cmdType">
        <a-radio value="write"> 写寄存器方式 </a-radio>
        <a-radio value="scan"> 扫描方式 </a-radio>
      </a-radio-group>
      <div style="margin-top: 15px">
        <div v-if="service.cmdType == 'scan'">
          开始位置：<a-input-number v-model="scan.begin"></a-input-number>
          个数：<a-input-number v-model="scan.count"></a-input-number>
          功能码:<a-select style="width: 200px" v-model="scan.code">
            <a-select-option
              v-for="item in codeList"
              :key="item.value"
              :value="item.value"
            >
              {{ item.title }}
            </a-select-option>
          </a-select>
        </div>
        <div v-else-if="service.cmdType == 'write'">
          <param-table :params="input_params"></param-table>
        </div>
        <div style="margin-top: 20px">
          <span>返回结果</span>
          <param-table :params="output_params"></param-table>
        </div>
      </div>
    </div>
  </div>
</template>
<script>
import ParamTable from "./ParamTable.vue";
export default {
  components: {
    ParamTable,
  },
  props: ["codeList", "service"],
  data() {
    return {
      cmdType: "write",

      pNames: [],
      input_params: [],
      scan: {},
      trueFalse: [
        { label: "是", value: true },
        { label: "否", value: false },
      ],
    };
  },
  watch: {
    service: {
      handler(nv) {
        let self = this;
        if (nv && nv.obj) {
          if (!nv.output_params) {
            nv.output_params = [];
          }
          if (!nv.input_params) {
            nv.input_params = [];
          }
          if (!nv.scan) {
            nv.scan = {};
          }
          self.cmdType = nv.cmdType;
          self.output_params = nv.output_params;
          self.input_params = nv.input_params;
          self.scan = nv.scan;
          self.getParams(nv.obj.input_data, self.input_params);
          self.getParams(nv.obj.output_data, self.output_params);
        }
      },
      deep: false,
      immediate: true,
    },
  },
  methods: {
    init(data) {

      if(data){
        if(data && !data.param){
          data.param = {}
        }
      }
      let row = data.param
      if (row) {
        if (!row.pid) {
          row.pid = "";
        }
        if (!row.sid) {
          row.sid = "";
        }
        if (!row.index) {
          row.index = 0;
        }
        if (!row.count) {
          row.count = 1;
        }
        if (row.offset == undefined) {
          row.offset = 0;
        }
        if (row.swap == undefined) {
          row.swap = true;
        }
        if (row.bigend == undefined) {
          if (this.cmdType == "write"){
            row.bigend = true;
          }else {
            row.bigend = false;
          }
        }
        if (row.func == undefined) {
          row.func = "";
        }
        if (row.begin == undefined) {
          row.begin = 0;
        }
        if (row.number == undefined) {
          row.number = 1.00;
        }
      }
    },
    onSelectedType() {},
    getParams(params, target) {
      params.forEach((d) => {
        let index = target.findIndex((w) => {
          return w.param_id == d.identifier;
        });
        if (index >= 0) {
          target[index].pname = d.name;

          this.init(target[index]);
          //this.input_params[index].
        } else {
          let item = { pname: d.name, param_id: d.identifier };
          this.init(item);
          target.push(item);
        }
      });
    },
  },
};
</script>
<style scoped></style>
